#!/usr/bin/groovy

if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'pre' || env.BRANCH_NAME == 'prod' ) {
  // load pipeline functions
  @Library('pipeline@master')

  // name: code项目名
  // bdm: 部署chart时用到的名, 根据不同的代码分支(注：不同的代码分支也代表了不同的要部署的环境)，通过helm -n <namespace> ls 可以查看
  // bnm: 根据分支来确定部署到不同的命名空间，
  // bim: 根据不同分支来区分不同环境的docker image 的子路径，如：harbor.yzh.com/yzhtech/[dev/prod/pre]/img_name:tag, 并且与chart中关于image定义的结构必须与之对应，否则部署会不成功
  // bem: 根据不同分支来区分不同的环境
  // deploy_name: 对bdm，也即对具体的分支进行取值
  // ns: 对bnm，也即对具体的分支进行取值
  // img_subpath: 对bim取值，
  // envType: 对bem取值，不同环境的节点标签选择器，需要事先对节点进行打标签。因为(开发/测试)共用一组主机资源池，此时标签统一为 envType=dev；(预发布/生产)共用一组主机资源池，此时标签统一为 envType=prod
  // githubAuth: 在jenkins上配置的可以通过ssh,从git仓库中拉取代码的认证ID
  // registryAuth: 在jenkins上配置的可以通过用户名和密码,从docker 仓库中拉取镜像的认证ID
  // chartRepoAuth: 在jenkins上配置的可以通过用户名和密码,从chart 仓库中拉取chart安装包的认证ID
  // node_label: jenkins slave 的名字前缀
  // project_repo_url: 项目代码git仓库的地址
  // registry: docker 镜像仓库的地址
  // chart_repo: chart仓库的地址
  // chart_name: chart 包本身的名字，也是docker image的名字（如果不包含子chart）
  // subChart_name: 子chart，这个一项要根据不同的子chart名来更改，同时也是子chart的docker image的名字

  def name = 'cmdb'
  def bdm = [master: 'cmdb-dev', prod: 'cmdb-prod', pre: 'cmdb-pre'] // branch to deployment map
  def bnm = [master: 'cmdb-dev', prod: 'prod', pre: 'pre'] // branch to namespace map
  def bim = [master: 'dev', prod: 'prod', pre: 'pre'] // branch to docker image map
  def bem = [master: 'dev', prod: 'prod', pre: 'prod'] // branch to deploy env map
  def deploy_name = bdm.get(env.BRANCH_NAME, 'test')
  def ns = bnm.get(env.BRANCH_NAME, 'test')
  def img_subpath = bim.get(env.BRANCH_NAME, 'test')
  def envType = bem.get(env.BRANCH_NAME, 'test')
  // send mail, eg: 'xxx@xxx.com,yyy@yyy.com'
  def email_list = 'yangzhen@yoozoo.com'

  // usually do not need to change the following variables
  // for git
  def githubAuth = 'mobgitlabauth'
  def project_repo_url = "git@gitlab.code.mob.com:web-developer/mobtech_cmdb.git"
  // for docker
  def registryAuth= 'mobregistryauth'
  def registry='10.6.103.204:80/mobtech'
  // for chart
  def chartRepoAuth = 'mobchartauth'
  def chart_repo = "http://10.6.103.204/chartrepo/mobtech"
  def chart_name = name
  def subChart_name = "mobtech-cmdb"
  // for jenkins slave
  def node_label = "cd4${name}"

  podTemplate(label: node_label, containers: [
        containerTemplate(
                name: 'jnlp',
                image: "dockerlab123/jnlp-slave:alpine",
                args: '${computer.jnlpmac} ${computer.name}'
        )],
        volumes: [
            hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
            secretVolume(mountPath: '$HOME/.kube', secretName: 'kubeconfig')
        ],
        imagePullSecrets: [ 'regsecret' ],
        nodeSelector:"envType=jenkins"
  ) {
      node(node_label) {
        def ppl = new com.mobtech.ci.Pipeline()
        try {
            def image_tag = buildImage name: subChart_name, img_subpath:img_subpath,
                  project_repo_url:project_repo_url,
                  githubAuth:githubAuth,
                  //node_label:node_label,
                  registry:registry,
                  registryAuth:registryAuth

            deployChart name: name, ns:ns,
                  deploy_name:deploy_name,
                  chartRepoAuth: chartRepoAuth,
                  repo:chart_repo,
                  chart_name:chart_name,
                  //node_label:node_label,
                  // 当同一个chart 包含多个子chart的时候，wait 和 set 都需要单独指定，因为资源类型和镜像会不同
                  wait: ["deploy/${subChart_name}"],
                  //set: ["${subChart_name}.image.tag": image_tag, "${subChart_name}.nodeSelector.envType": envType]
                  set: ["${subChart_name}.image.name": "${img_subpath}/${subChart_name}","${subChart_name}.image.tag": image_tag, "${subChart_name}.nodeSelector.envType": envType]
            //currentBuild.result = "SUCCESS"
        }
        catch (Exception ex) {
          currentBuild.result = "FAILED"
          throw ex
        }
        finally{
          sendMail(email_list)
        }
     }
  }
}
